package com.sitem.appsjam;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.sitem.appsjam.Managers.LayerManager;
import com.sitem.appsjam.Managers.LayerManager.OnRefreshFinished;
import com.sitem.appsjam.Managers.LocManager;

public class Login extends Activity {
	private final static int DIALOG_NETWORK_ERROR = 1;
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id) {
			case DIALOG_NETWORK_ERROR:
				return new AlertDialog.Builder(this)
				.setTitle(R.string.error)
				.setMessage(R.string.conexion_error)
				.setPositiveButton(R.string.retry, new AlertDialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						reload();
					}
				})
				.setNegativeButton(R.string.exit,  new AlertDialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).create();
			default:
				return super.onCreateDialog(id);
		}
		
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		ImageButton b;
		b = (ImageButton)findViewById(R.id.but_ar);
		b.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Login.this,ARViewer.class));
			}
		});
		b = (ImageButton)findViewById(R.id.but_map);
		b.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Login.this,LocationWays.class));
			}
		});
		LayerManager.listener = new OnRefreshFinished() {
			@Override
			public void onFinish(boolean success) {
				findViewById(R.id.login_progress).setVisibility(View.GONE);
				findViewById(R.id.gone1).setVisibility(View.VISIBLE);
				findViewById(R.id.gone2).setVisibility(View.VISIBLE);
			}
		};
		LocManager.init(this);
		LayerManager.init(this);
	}
	
	private void reload() {
		Intent intent = getIntent();
		overridePendingTransition(0, 0);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		finish();
		overridePendingTransition(0, 0);
		startActivity(intent);
	}


}
